<?php
class ChartsController extends AppController {
	public function index() {
		$this->layout = 'pharmarket';
		$this->loadModel('Row');
		$this->loadModel('Category');
		$this->loadModel('Product');
		$this->loadModel('User');
		$categoriesAll = $this->Category->find('all');
		$products = $this->Product->find('all');
		$rows = $this->Row->find('all');
		

		/*
		 * Calcul des meilleures ventes
		 */
		$bestventes = [];
		for($i=0; $i < sizeof($rows); $i++){
			if(array_key_exists($rows[$i]['Product']['nom'], $bestventes)){
				$bestventes[$rows[$i]['Product']['nom']] += $rows[$i]['Row']['quantite'];
			}else{
				$bestventes[$rows[$i]['Product']['nom']] = $rows[$i]['Row']['quantite'];
			}
			
		}
		arsort($bestventes);
		$bestventesKeys ="[";
		$bestventesVals = "[";
		$i = 0;
		foreach ($bestventes as $key => $val) {
			if($i == 10){
				break;
			}

			if($i==0){
    			$bestventesKeys = $bestventesKeys . "\"" . $key . "\"";
    			$bestventesVals = $bestventesVals . $val;
    		}else{
    			$bestventesKeys = $bestventesKeys . "," . "\"" . $key . "\"";
    			$bestventesVals = $bestventesVals . "," . $val;
    		}
    		$i++;
		}
		$bestventesKeys .= "]";
		$bestventesVals .= "]";


		/*
		 * Calcul des ventes par categories
		 */
		$ventesCategories = [];
		for($i=0; $i < sizeof($categoriesAll); $i++){
			$ventesCategories[$categoriesAll[$i]['Category']['id']]['nom'] =  $categoriesAll[$i]['Category']['nom'];
			$ventesCategories[$categoriesAll[$i]['Category']['id']]['ventes'] = 0;
		}

		for($i=0; $i < sizeof($rows); $i++){
			$ventesCategories[$rows[$i]['Product']['category_id']]['ventes'] += $rows[$i]['Row']['quantite'];
		}

		$ventesCategoriesKeys ="[";
		$ventesCategoriesVals = "[";
		$i = 0;
		foreach ($ventesCategories as $key => $val) {
			if($i == 10){
				break;
			}

			if($i==0){
    			$ventesCategoriesKeys .= "\"" . $val['nom'] . "\"";
    			$ventesCategoriesVals .= $val['ventes'];
    		}else{
    			$ventesCategoriesKeys .= "," . "\"" . $val['nom'] . "\"";
    			$ventesCategoriesVals .= "," . $val['ventes'];
    		}
    		$i++;
		}
		$ventesCategoriesKeys .= "]";
		$ventesCategoriesVals .= "]";

		/*
		 * Nombre de produit par categorie
		 */
		$produitCategories = [];
		for($i=0; $i < sizeof($categoriesAll); $i++){
			$produitCategories[$categoriesAll[$i]['Category']['id']]['nom'] =  $categoriesAll[$i]['Category']['nom'];
			$produitCategories[$categoriesAll[$i]['Category']['id']]['nbr'] = 0;
		}

		for($i=0; $i < sizeof($products); $i++){
			$produitCategories[$products[$i]['Product']['category_id']]['nbr'] += 1;
		}

		$produitCategoriesKeys ="[";
		$produitCategoriesVals = "[";
		$i = 0;
		foreach ($produitCategories as $key => $val) {
			if($i == 10){
				break;
			}

			if($i==0){
    			$produitCategoriesKeys .= "\"" . $val['nom'] . "\"";
    			$produitCategoriesVals .= $val['nbr'];
    		}else{
    			$produitCategoriesKeys .= "," . "\"" . $val['nom'] . "\"";
    			$produitCategoriesVals .= "," . $val['nbr'];
    		}
    		$i++;
		}
		$produitCategoriesKeys .= "]";
		$produitCategoriesVals .= "]";

		/*
		 * Nombre de ventes par pays
		 */
		$ventesPays = [];
		for($i=0; $i < sizeof($rows); $i++){
			if(array_key_exists($rows[$i]['User']['pays'], $ventesPays)){
				$ventesPays[$rows[$i]['User']['pays']] += $rows[$i]['Row']['quantite'];
			}else{
				$ventesPays[$rows[$i]['User']['pays']] = $rows[$i]['Row']['quantite'];
			}
			
		}

		$ventesPaysKeys ="[";
		$ventesPaysVals = "[";
		$i = 0;
		foreach ($ventesPays as $key => $val) {
			if($i==0){
    			$ventesPaysKeys .= "\"" . $key . "\"";
    			$ventesPaysVals .= $val;
    		}else{
    			$ventesPaysKeys .= "," . "\"" . $key . "\"";
    			$ventesPaysVals .= "," . $val;
    		}
    		$i++;
		}
		$ventesPaysKeys .= "]";
		$ventesPaysVals .= "]";


		/*
		 * Moyenne age pour chaque categorie
		 */
		$ageCategories = [];
		for($i=0; $i < sizeof($categoriesAll); $i++){
			$ageCategories[$categoriesAll[$i]['Category']['id']]['nom'] =  $categoriesAll[$i]['Category']['nom'];
			$ageCategories[$categoriesAll[$i]['Category']['id']]['age'] = 0;
			$ageCategories[$categoriesAll[$i]['Category']['id']]['count'] = 0;

		}

		for($i=0; $i < sizeof($rows); $i++){
			$ageCategories[$rows[$i]['Product']['category_id']]['age'] += (int) ((time() - strtotime($rows[$i]['User']['date_naissance'])) / 3600 / 24 / 365);
			$ageCategories[$rows[$i]['Product']['category_id']]['count'] += 1;
		}

		
		$ageCategoriesKeys ="[";
		$ageCategoriesVals = "[";
		$i = 0;
		foreach ($ageCategories as $key => $val) {
			if($i==0){
    			$ageCategoriesKeys .= "\"" . $val['nom'] . "\"";
    			if($val['count'] != 0){
    				$ageCategoriesVals .= (int) ($val['age'] / $val['count']);
    			}else{
    				$ageCategoriesVals .= '0';
    			}
    		}else{
    			$ageCategoriesKeys .= "," . "\"" . $val['nom'] . "\"";
    			if($val['count'] != 0){
    				$ageCategoriesVals .= "," . (int) ($val['age'] / $val['count']);
    			}else{
    				$ageCategoriesVals .= ",0";
    			}
    		}
    		$i++;
		}
		$ageCategoriesKeys .= "]";
		$ageCategoriesVals .= "]";
		


		$this->set('bvKeys', $bestventesKeys);
		$this->set('bvVals', $bestventesVals);
		$this->set('vcKeys', $ventesCategoriesKeys);
		$this->set('vcVals', $ventesCategoriesVals);
		$this->set('vpKeys', $ventesPaysKeys);
		$this->set('vpVals', $ventesPaysVals);
		$this->set('pcKeys', $produitCategoriesKeys);
		$this->set('pcVals', $produitCategoriesVals);
		$this->set('acKeys', $ageCategoriesKeys);
		$this->set('acVals', $ageCategoriesVals);
		$this->set('rows', $ageCategories);
		$this->set('categories', $ventesCategories);
	}
}